/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainScreen.java
 *
 * Created on Jun 17, 2012, 3:31:12 PM
 */

package com.apps.ui;

import com.apps.controllers.SearchController;
import com.apps.utils.StartupHelper;
import java.io.File;
import java.util.Calendar;
import javax.swing.UIManager;

/**
 *
 * @author ARIJIT PAL
 */
public class MainScreen extends javax.swing.JFrame {

    Settings settingsUI;
    AccountSetupScreen accUI;
    About aboutUI;
    StatusScreen statusUI;
    
    /** Creates new form MainScreen */
    public MainScreen() {
        initComponents();
    }

    public void setSettingsUI(Settings settingScr){
        this.settingsUI = settingScr;
    }

    public void setAccUI(AccountSetupScreen accUI) {
        this.accUI = accUI;
    }

    public void setAboutUI(About aboutUI) {
        this.aboutUI = aboutUI;
    }
    
    public void setStatusUI(StatusScreen statusUI){
        this.statusUI = statusUI;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        searchText = new javax.swing.JTextField();
        searchBtn = new javax.swing.JButton();
        acSetupBtn = new javax.swing.JButton();
        aboutBtn = new javax.swing.JButton();
        settingsBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        status = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/apps/ui/resources/LogEx.png"))); // NOI18N
        jPanel2.add(jLabel1);

        jPanel1.add(jPanel2);

        jLabel2.setText("      ");
        jPanel1.add(jLabel2);

        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        jLabel3.setFont(new java.awt.Font("Calibri", 1, 24)); // NOI18N
        jLabel3.setText("Search Key : ");

        searchText.setFont(new java.awt.Font("Calibri", 1, 24)); // NOI18N
        searchText.setForeground(new java.awt.Color(102, 102, 102));
        searchText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        searchText.setText("Enter Search Key");
        searchText.setToolTipText("Enter Application# or some text");
        searchText.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        searchText.setMargin(new java.awt.Insets(5, 5, 5, 5));
        searchText.setMinimumSize(new java.awt.Dimension(12, 100));

        searchBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/apps/ui/resources/find.png"))); // NOI18N
        searchBtn.setText("Extract Log");
        searchBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                searchBtnMouseClicked(evt);
            }
        });

        acSetupBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/apps/ui/resources/acsetup.png"))); // NOI18N
        acSetupBtn.setText("A/C Setup");
        acSetupBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acSetupBtnActionPerformed(evt);
            }
        });

        aboutBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/apps/ui/resources/info.png"))); // NOI18N
        aboutBtn.setText("About LogEx");
        aboutBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutBtnActionPerformed(evt);
            }
        });

        settingsBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/apps/ui/resources/settings.png"))); // NOI18N
        settingsBtn.setText("Settings");
        settingsBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                settingsBtnActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel4.setText("Logger info : ");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(searchText, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(244, 244, 244)
                        .addComponent(searchBtn))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(acSetupBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(settingsBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(aboutBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(searchText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(searchBtn)
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(acSetupBtn)
                    .addComponent(aboutBtn)
                    .addComponent(settingsBtn))
                .addGap(7, 7, 7)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
        );

        jPanel3.add(jPanel4);

        jPanel1.add(jPanel3);

        status.setText("Status");
        status.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                statusMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addGap(0, 542, Short.MAX_VALUE)
                .addComponent(status))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(status)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 588, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void settingsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingsBtnActionPerformed
        // TODO add your handling code here:
        try{
            if(settingsUI == null){
                settingsUI = new Settings();
            }
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    settingsUI.setVisible(true);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_settingsBtnActionPerformed

    private void acSetupBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acSetupBtnActionPerformed
        // TODO add your handling code here:
        try{
            if(accUI == null){
                accUI = new AccountSetupScreen();
            }
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    accUI.setVisible(true);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_acSetupBtnActionPerformed

    private void aboutBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutBtnActionPerformed
        // TODO add your handling code here:
        try{
            if(aboutUI == null){
                aboutUI = new About();
            }
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    aboutUI.setVisible(true);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_aboutBtnActionPerformed

    private void statusMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_statusMouseClicked
        // TODO add your handling code here:
        try{
            if(statusUI == null){
                System.out.println("new status UI created");
                statusUI = new StatusScreen();
            }
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    statusUI.setVisible(true);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_statusMouseClicked

    private void searchBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchBtnMouseClicked
        // TODO add your handling code here:
        SearchResultScreen searchScreen = new SearchResultScreen(statusUI.getPanel());
        String localDir = StartupHelper.getSettings().getApps_result_path()
                +File.separator+searchText.getText().split(" ")[0]
                +Calendar.getInstance().getTimeInMillis()+"0"+(int)(Math.random()*10000);
        //this.setVisible(false);
        searchScreen.setVisible(true);
        SearchController.search(searchText.getText(), localDir, searchScreen);
        //this.setVisible(true);
        //searchScreen = null;
    }//GEN-LAST:event_searchBtnMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    new MainScreen().setVisible(true);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aboutBtn;
    private javax.swing.JButton acSetupBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton searchBtn;
    private javax.swing.JTextField searchText;
    private javax.swing.JButton settingsBtn;
    private javax.swing.JLabel status;
    // End of variables declaration//GEN-END:variables

}
